<!-- 提交报名 -->
<template>
	<view class="submit-wrapper">
		<block-view>
			<view class="title">队员信息收集偏好</view>
			<view class="note">以下是活动发起方希望您填写的报名信息，以便给您带来更好的活动体验：</view>
			<view class="ipt-title">{{ preference }}</view>
			<textarea class="desc" placeholder="请按要求填写相应信息" v-model="like_member"></textarea>
		</block-view>
	</view>
	<fixed-footer>
		<primary-btn title="提交报名" @btnClick="submitApply"></primary-btn>
	</fixed-footer>
</template>

<script>
	import { wxPay } from '../../network/pay'
	import { useUserStore } from '../../store/userStore'
	
	export default {
		data() {
			return {
				userStore: useUserStore(),
				preference: '',
				like_member: '',
				price_level: [],
				activityId: 0,
				delta_num: 2,
				reward: {},//奖励
			}
		},
		onLoad(options) {
			this.activityId = options.activityId
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('preferenceInfo', (data) => {
				this.price_level = data.price_level;
				this.preference = data.preference;
				this.reward = data.reward;
				//this.delta_num = this.delta_num + 2;
			})
		},
		methods: {
			async submitApply() {
				if (!this.like_member) {
					uni.showToast({
						title: '请按要求填写相应信息',
						icon: 'none'
					})
					return
				}
				var link_url = '/pages/detail/index?activityId='+ this.activityId+"&isCheckCode=true&applySuccess=true";
				const res = await this.$request.post('/api/activity/enroll', {
					activity_id: this.activityId,
					price_level: this.price_level,
					like_member: this.like_member,
					reward_no: Object.keys(this.reward).length > 0 ? this.reward.reward_no : "",
				})
				
				if (res.code == 200) {
					if (!res.data) {
						uni.showToast({
							title: '报名成功',
							icon: 'success'
						})
						uni.$emit('applySuccess')
						setTimeout(() => {
							uni.navigateTo({
								url: link_url
							})
							// uni.navigateBack({
							// 	delta: this.delta_num
							// })
						}, 1500)
						return
					}
					const { order_no , pay_price} = res.data
					if (pay_price > 0) {
						const data = {
							pay_type: 1,
							order_no,
							client: 1,
							openid: this.userStore.openid
						}
						wxPay('/api/pay/pay', data).then((payRes) => {
							uni.showToast({
								title: '报名成功',
								icon: 'success'
							})
							uni.$emit('applySuccess')
							setTimeout(() => {
								uni.navigateTo({
									url: link_url
								})
								// uni.navigateBack({
								// 	delta: this.delta_num
								// })
							}, 1500)
						}).catch((error) => {
							uni.showToast({
								title: '支付失败，可在订单管理继续完成支付',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url: link_url
								})
								// uni.navigateBack({
								// 	delta: this.delta_num
								// })
							}, 1500)
						})
					}else{
						uni.showToast({
							title: '报名成功',
							icon: 'success'
						})
						uni.$emit('applySuccess')
						setTimeout(() => {
							uni.navigateTo({
								url: link_url
							})
							// uni.navigateBack({
							// 	delta: this.delta_num
							// })
						}, 1500);
					}

				}
			}
 		}
	}
</script>

<style scoped lang="scss">
	.submit-wrapper {
		padding: 24rpx 30rpx;
		
		.title {
			font-size: 32rpx;
			font-weight: bold;
		}
		.note {
			font-size: 24rpx;
			color: #999;
			margin-top: 20rpx;
		}
		.ipt-title {
			margin-top: 32rpx;
		}
		.desc {
			width: 100%;
			box-sizing: border-box;
			margin-top: 20rpx;
			border: 1px solid $uni-border-color;
			border-radius: 20rpx;
			height: 300rpx;
			font-size: 28rpx;
			padding: 24rpx 20rpx;
		}
	}
</style>